﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ATCSim.Objects
{
    class HoldingZones
    {
        private Random rand = new Random(DateTime.Now.Millisecond);
        private List<Aircraft> _AicraftInHolding = new List<Aircraft>();
        private List<Aircraft> _AicraftMovingIntoAirspace = new List<Aircraft>();
        private AircraftManager _AirManager;

        bool H1 = false;
        bool H2 = false;
        bool H3 = false;
        bool H4 = false;

        public HoldingZones(AircraftManager AircraftManager )
        {
            _AirManager = AircraftManager;
        }
        public bool Add(String AicraftID)
        {
            if (H1 == false)
            {
                H1 = true;
                _AirManager.AircraftList.Add(new Aircraft(AicraftID, new PointF(335,90), 360, rand.Next(1000, 10000), "A"));
                _AicraftInHolding.Add(_AirManager.FindAircraft(AicraftID));
                return true;
            }
            if (H2 == false)
            {
                H2 = true;
                _AirManager.AircraftList.Add(new Aircraft(AicraftID, new PointF(335, 630), 360, rand.Next(1000, 10000), "A"));
                _AicraftInHolding.Add(_AirManager.FindAircraft(AicraftID));
                return true;
            }
            if (H3 == false)
            {
                H3 = true;
                _AirManager.AircraftList.Add(new Aircraft(AicraftID, new PointF(875, 630), 360, rand.Next(1000, 10000), "A"));
                _AicraftInHolding.Add(_AirManager.FindAircraft(AicraftID));
                return true;
            }
            if (H4 == false)
            {
                H4 = true;
                _AirManager.AircraftList.Add(new Aircraft(AicraftID, new PointF(875, 90), 360, rand.Next(1000, 10000), "A"));
                _AicraftInHolding.Add(_AirManager.FindAircraft(AicraftID));
                return true;
            }
            return false;
        }
        public void MoveAircraftIntoAirspace(Aircraft SelectedAircraft)
        {
            if (Math.Pow(SelectedAircraft.Position.X + 7.5 - 290, 2) + Math.Pow(SelectedAircraft.Position.Y + 7.5 - 90, 2) < Math.Pow(80, 2))
            {
                _AicraftMovingIntoAirspace.Add(_AirManager.FindAircraft(SelectedAircraft.ID));
                _AicraftInHolding.Remove(SelectedAircraft);
                H1 = false;
            }
            if (Math.Pow(SelectedAircraft.Position.X + 7.5 - 290, 2) + Math.Pow(SelectedAircraft.Position.Y + 7.5 - 630, 2) < Math.Pow(80, 2))
            {
                _AicraftMovingIntoAirspace.Add(_AirManager.FindAircraft(SelectedAircraft.ID));
                _AicraftInHolding.Remove(SelectedAircraft);
                H2 = false;
            }
            if (Math.Pow(SelectedAircraft.Position.X + 7.5 - 940, 2) + Math.Pow(SelectedAircraft.Position.Y + 7.5 - 630, 2) < Math.Pow(80, 2))
            {
                _AicraftMovingIntoAirspace.Add(_AirManager.FindAircraft(SelectedAircraft.ID));
                _AicraftInHolding.Remove(SelectedAircraft);
                H3 = false;
            }
            if (Math.Pow(SelectedAircraft.Position.X + 7.5 - 940, 2) + Math.Pow(SelectedAircraft.Position.Y + 7.5 - 90, 2) < Math.Pow(80, 2))
            {
                _AicraftMovingIntoAirspace.Add(_AirManager.FindAircraft(SelectedAircraft.ID));
                _AicraftInHolding.Remove(SelectedAircraft);
                H4 = false;
            }
        }
        public void MoveAircraftInHoldingZoneUpdate()
        {
            for (int i = 0; i < _AicraftInHolding.Count; i++)
            {
                if (Math.Pow(_AicraftInHolding.ElementAt(i).Position.X + 7.5 - 290, 2) + Math.Pow(_AicraftInHolding.ElementAt(i).Position.Y + 7.5 - 90, 2) < Math.Pow(80, 2))
                {
                    if (_AicraftInHolding.ElementAt(i).Position.Y < 50)
                    {
                        _AicraftInHolding.ElementAt(i).ToHeading = 270;
                        _AicraftInHolding.ElementAt(i).TurnLeft = true;
                    }
                    if (_AicraftInHolding.ElementAt(i).Position.X < 265)
                    {
                        _AicraftInHolding.ElementAt(i).ToHeading = 180;
                        _AicraftInHolding.ElementAt(i).TurnLeft = true;
                    }
                    if (_AicraftInHolding.ElementAt(i).Position.Y > 115)
                    {
                        _AicraftInHolding.ElementAt(i).ToHeading = 90;
                        _AicraftInHolding.ElementAt(i).TurnLeft = true;
                    }
                    if (_AicraftInHolding.ElementAt(i).Position.X > 305 && _AicraftInHolding.ElementAt(i).Position.Y >= 115)
                    {
                        _AicraftInHolding.ElementAt(i).ToHeading = 360;
                        _AicraftInHolding.ElementAt(i).TurnLeft = true;
                    }
                }
                if (Math.Pow(_AicraftInHolding.ElementAt(i).Position.X + 7.5 - 290, 2) + Math.Pow(_AicraftInHolding.ElementAt(i).Position.Y + 7.5 - 630, 2) < Math.Pow(80, 2))
                {
                    if (_AicraftInHolding.ElementAt(i).Position.Y < 600)
                    {
                        _AicraftInHolding.ElementAt(i).ToHeading = 270;
                        _AicraftInHolding.ElementAt(i).TurnLeft = true;
                    }
                    if (_AicraftInHolding.ElementAt(i).Position.X < 250)
                    {
                        _AicraftInHolding.ElementAt(i).ToHeading = 180;
                        _AicraftInHolding.ElementAt(i).TurnLeft = true;
                    }
                    if (_AicraftInHolding.ElementAt(i).Position.Y > 650)
                    {
                        _AicraftInHolding.ElementAt(i).ToHeading = 90;
                        _AicraftInHolding.ElementAt(i).TurnLeft = true;
                    }
                    if (_AicraftInHolding.ElementAt(i).Position.X > 315 && _AicraftInHolding.ElementAt(i).Position.Y >= 650)
                    {
                        _AicraftInHolding.ElementAt(i).ToHeading = 360;
                        _AicraftInHolding.ElementAt(i).TurnLeft = true;
                    }
                }
                if (Math.Pow(_AicraftInHolding.ElementAt(i).Position.X + 7.5 - 940, 2) + Math.Pow(_AicraftInHolding.ElementAt(i).Position.Y + 7.5 - 630, 2) < Math.Pow(80, 2))
                {
                    if (_AicraftInHolding.ElementAt(i).Position.Y < 600)
                    {
                        _AicraftInHolding.ElementAt(i).ToHeading = 90;
                        _AicraftInHolding.ElementAt(i).TurnLeft = false;
                    }
                    if (_AicraftInHolding.ElementAt(i).Position.X > 965)
                    {
                        _AicraftInHolding.ElementAt(i).ToHeading = 180;
                        _AicraftInHolding.ElementAt(i).TurnLeft = false;
                    }
                    if (_AicraftInHolding.ElementAt(i).Position.Y > 650)
                    {
                        _AicraftInHolding.ElementAt(i).ToHeading = 270;
                        _AicraftInHolding.ElementAt(i).TurnLeft = false;
                    }
                    if (_AicraftInHolding.ElementAt(i).Position.X < 900 && _AicraftInHolding.ElementAt(i).Position.Y >= 650)
                    {
                        _AicraftInHolding.ElementAt(i).ToHeading = 360;
                        _AicraftInHolding.ElementAt(i).TurnLeft = false;
                    }
                }
                if (Math.Pow(_AicraftInHolding.ElementAt(i).Position.X + 7.5 - 940, 2) + Math.Pow(_AicraftInHolding.ElementAt(i).Position.Y + 7.5 - 90, 2) < Math.Pow(80, 2))
                {
                    if (_AicraftInHolding.ElementAt(i).Position.Y < 55)
                    {
                        _AicraftInHolding.ElementAt(i).ToHeading = 90;
                        _AicraftInHolding.ElementAt(i).TurnLeft = false;
                    }
                    if (_AicraftInHolding.ElementAt(i).Position.X > 965)
                    {
                        _AicraftInHolding.ElementAt(i).ToHeading = 180;
                        _AicraftInHolding.ElementAt(i).TurnLeft = false;
                    }
                    if (_AicraftInHolding.ElementAt(i).Position.Y > 115)
                    {
                        _AicraftInHolding.ElementAt(i).ToHeading = 270;
                        _AicraftInHolding.ElementAt(i).TurnLeft = false;
                    }
                    if (_AicraftInHolding.ElementAt(i).Position.X < 900 && _AicraftInHolding.ElementAt(i).Position.Y >= 115)
                    {
                        _AicraftInHolding.ElementAt(i).ToHeading = 360;
                        _AicraftInHolding.ElementAt(i).TurnLeft = false;
                    }
                }
            }
        }
        public void FlyIntoAirspaceUpdate()
        {
            for (int i = 0; i < _AicraftMovingIntoAirspace.Count; i++)
            {
                if (_AicraftMovingIntoAirspace.ElementAt(i).Position.X > 210 && _AicraftMovingIntoAirspace.ElementAt(i).Position.X < 370 && _AicraftMovingIntoAirspace.ElementAt(i).Position.Y < 170)
                {
                    _AicraftMovingIntoAirspace.ElementAt(i).ToHeading = 270;
                    _AicraftMovingIntoAirspace.ElementAt(i).TurnLeft = true;
                }
                if (_AicraftMovingIntoAirspace.ElementAt(i).Position.X < 240 && _AicraftMovingIntoAirspace.ElementAt(i).Position.X < 370 && _AicraftMovingIntoAirspace.ElementAt(i).Position.Y < 170)
                {
                    _AicraftMovingIntoAirspace.ElementAt(i).ToHeading = 180;
                    _AicraftMovingIntoAirspace.ElementAt(i).TurnLeft = true;
                }
                if (_AicraftMovingIntoAirspace.ElementAt(i).Position.Y > 330 && _AicraftMovingIntoAirspace.ElementAt(i).Position.X < 370 && _AicraftMovingIntoAirspace.ElementAt(i).Position.Y < 335)
                {
                    _AicraftMovingIntoAirspace.ElementAt(i).ToHeading = 90;
                    _AicraftMovingIntoAirspace.ElementAt(i).TurnLeft = true;
                    _AicraftMovingIntoAirspace.RemoveAt(i);
                    return;
                }

                if (_AicraftMovingIntoAirspace.ElementAt(i).Position.X > 210 && _AicraftMovingIntoAirspace.ElementAt(i).Position.X < 370 && _AicraftMovingIntoAirspace.ElementAt(i).Position.Y > 550)
                {
                    _AicraftMovingIntoAirspace.ElementAt(i).ToHeading = 270;
                    _AicraftMovingIntoAirspace.ElementAt(i).TurnLeft = true;
                }
                if (_AicraftMovingIntoAirspace.ElementAt(i).Position.X < 240 && _AicraftMovingIntoAirspace.ElementAt(i).Position.X < 370 && _AicraftMovingIntoAirspace.ElementAt(i).Position.Y > 550)
                {
                    _AicraftMovingIntoAirspace.ElementAt(i).ToHeading = 360;
                    _AicraftMovingIntoAirspace.ElementAt(i).TurnLeft = false;
                }
                if (_AicraftMovingIntoAirspace.ElementAt(i).Position.Y < 385 && _AicraftMovingIntoAirspace.ElementAt(i).Position.X < 370 && _AicraftMovingIntoAirspace.ElementAt(i).Position.Y > 380)
                {
                    _AicraftMovingIntoAirspace.ElementAt(i).ToHeading = 90;
                    _AicraftMovingIntoAirspace.ElementAt(i).TurnLeft = false;
                    _AicraftMovingIntoAirspace.RemoveAt(i);
                    return;
                }

                if (_AicraftMovingIntoAirspace.ElementAt(i).Position.X < 965 && _AicraftMovingIntoAirspace.ElementAt(i).Position.X > 860 && _AicraftMovingIntoAirspace.ElementAt(i).Position.Y > 550)
                {
                    _AicraftMovingIntoAirspace.ElementAt(i).ToHeading = 90;
                    _AicraftMovingIntoAirspace.ElementAt(i).TurnLeft = false;
                }
                if (_AicraftMovingIntoAirspace.ElementAt(i).Position.X > 965 && _AicraftMovingIntoAirspace.ElementAt(i).Position.X > 860 && _AicraftMovingIntoAirspace.ElementAt(i).Position.Y > 550)
                {
                    _AicraftMovingIntoAirspace.ElementAt(i).ToHeading = 360;
                    _AicraftMovingIntoAirspace.ElementAt(i).TurnLeft = true;
                }
                if (_AicraftMovingIntoAirspace.ElementAt(i).Position.Y < 380 && _AicraftMovingIntoAirspace.ElementAt(i).Position.X > 860 && _AicraftMovingIntoAirspace.ElementAt(i).Position.Y > 375)
                {
                    _AicraftMovingIntoAirspace.ElementAt(i).ToHeading = 270;
                    _AicraftMovingIntoAirspace.ElementAt(i).TurnLeft = true;
                    _AicraftMovingIntoAirspace.RemoveAt(i);
                    return;
                }

                if (_AicraftMovingIntoAirspace.ElementAt(i).Position.X < 965 && _AicraftMovingIntoAirspace.ElementAt(i).Position.X > 860 && _AicraftMovingIntoAirspace.ElementAt(i).Position.Y < 170)
                {
                    _AicraftMovingIntoAirspace.ElementAt(i).ToHeading = 90;
                    _AicraftMovingIntoAirspace.ElementAt(i).TurnLeft = false;
                }
                if (_AicraftMovingIntoAirspace.ElementAt(i).Position.X > 965 && _AicraftMovingIntoAirspace.ElementAt(i).Position.X > 860 && _AicraftMovingIntoAirspace.ElementAt(i).Position.Y < 170)
                {
                    _AicraftMovingIntoAirspace.ElementAt(i).ToHeading = 180;
                    _AicraftMovingIntoAirspace.ElementAt(i).TurnLeft = false;
                }
                if (_AicraftMovingIntoAirspace.ElementAt(i).Position.Y > 330 && _AicraftMovingIntoAirspace.ElementAt(i).Position.X > 860 && _AicraftMovingIntoAirspace.ElementAt(i).Position.Y < 335)
                {
                    _AicraftMovingIntoAirspace.ElementAt(i).ToHeading = 270;
                    _AicraftMovingIntoAirspace.ElementAt(i).TurnLeft = false;
                    _AicraftMovingIntoAirspace.RemoveAt(i);
                    return;
                }
            }
        }
    }
}
